//
//  AppDelegate.m
//  YZZX
//
//  Created by Easaa on 15/3/20.
//  Copyright (c) 2015年 Easaa. All rights reserved.
//

#import "AppDelegate.h"
#import "HXRootViewController.h"
#import "LeftViewVC.h"
#import "ReadTheMessageVIew.h"




@interface AppDelegate ()

@end

@implementation AppDelegate
{
    HXHttpRequest * _httpRequest;
    
    NSMutableArray *ColumnModelArray;
    
     HXRootViewController *rootview;

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; //状态栏白色的
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
  //设置文章字体大小
    NSUserDefaults *UserDefaultSize =[NSUserDefaults standardUserDefaults];
    [UserDefaultSize setFloat:16.0 forKey:YZZX_Font_Size];
    

    [UserDefaultSize setObject:@"" forKey:YZZX_Login_name_zhuce ];
    [UserDefaultSize setObject:@"" forKey:YZZX_Login_password_zhuce ];
    
    
//    //设置//用户名
//   
//    [UserDefaultSize setObject:nil forKey:YZZX_Login_fullname];
//    
//    //设置//用户id
//    
//    [UserDefaultSize setObject:nil forKey:YZZX_Login_user_id];
//    
//
//    //设置//站点id
//    
//    [UserDefaultSize setObject:nil forKey:YZZX_Login_site_id];
    
 
    //tabbar
    rootview = [[HXRootViewController alloc]init];
    
    
    //导航栏
    
    SuperNavVC *NaVC = [[SuperNavVC alloc]initWithRootViewController:rootview];
    
 
    
    self.window.rootViewController = NaVC;
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    
    
    
    
    /**********************************************************/      //极光光推送
        [self Jpush:(NSDictionary *)launchOptions];

    
    
    return YES;
}









-(void)Jpush:(NSDictionary *)launchOptions
{
    
    
    //推送 模块
    
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //可以添加自定义categories
        [APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                       UIUserNotificationTypeSound |
                                                       UIUserNotificationTypeAlert)
                                           categories:nil];
    } else {
        //categories 必须为nil
        [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                       UIRemoteNotificationTypeSound |
                                                       UIRemoteNotificationTypeAlert)
                                           categories:nil];
    }
#else
    //categories 必须为nil
    [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                   UIRemoteNotificationTypeSound |
                                                   UIRemoteNotificationTypeAlert)
                                       categories:nil];
#endif
    // Required
    [APService setupWithOption:launchOptions];
    
    
    //     [self showPushNews:[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]];
    [self showPushNews:launchOptions];
}
-(void)showPushNews:(NSDictionary *)sendDictionary
{
    
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:-1];//让图标消失
    
//    if ([sendDictionary objectForKey:TYPE] != nil)
//    {
//        switch ([[sendDictionary objectForKey:TYPE] integerValue])
//        {
//            case 5://直播
//            {
//                DLog(@"点击跳转访谈");
//                ZZInterviewViewController *interviewViewController = [[ZZInterviewViewController alloc] initWithStuioDictionary:@{LIVEID_RECEIVER: [sendDictionary objectForKey:NEWSID] /*[contentArray objectAtIndex:0]*/}];
//                [navigationController pushViewController:interviewViewController animated:YES];
//            }
//                break;
//            case 10://图集
//            {
//                DLog(@"点击跳转图集");
//                ZZAtlasDetailViewController *atlasDetailViewController = [[ZZAtlasDetailViewController alloc]initWithDataDictionary:@{ALBUMID: [sendDictionary objectForKey:NEWSID]/*[contentArray objectAtIndex:0]*/}];
//                [navigationController pushViewController:atlasDetailViewController animated:YES];
//            }
//                break;
//            case 11://专题
//            {
//                DLog(@"点击跳转专题新闻");
//                ZZNewsSpecialTopicViewController1 *newsSpecialTopicViewController = [[ZZNewsSpecialTopicViewController1 alloc] initWithDataDictionary:@{CONNECTID: [sendDictionary objectForKey:NEWSID]/*[contentArray objectAtIndex:0]*/}];
//                [navigationController pushViewController:newsSpecialTopicViewController animated:YES];
//            }
//                break;
//            case 12://早晚报
//            {
//                
//            }
//                break;
//                
//                
//            default:
//            {
//                DLog(@"点击跳转调查新闻");
//                XGLNewsDetailViewController *newsGambitViewController = [[XGLNewsDetailViewController alloc]initWithNewsDictionary:@{NEWSID: [sendDictionary objectForKey:NEWSID]/*[contentArray objectAtIndex:0]*/}];
//                [navigationController pushViewController:newsGambitViewController animated:YES];
//            }
//                break;
//        }
//    }
    
}


//iOS7新方法
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:
(void (^)(UIBackgroundFetchResult))completionHandler {
    [APService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
    
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    if (!_appIsActive)
    {
        [self dealAPNSNewsWith:userInfo];
    } else
    {
        self.appLaunchAPNNews = userInfo;
        NSDictionary *aps = [userInfo objectForKey:@"aps"];
        NSString *alert = [aps objectForKey:@"alert"];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"消息提示"
                                                            message:alert
                                                           delegate:self
                                                  cancelButtonTitle:@"忽略"
                                                  otherButtonTitles:@"查看", nil];
        [alertView show];
    }
}



- (void)dealAPNSNewsWith:(NSDictionary *)userInfo
{
    NSString *strAction = [userInfo objectForKey:@"action"];
    if (!StringIsEmpty(strAction))
    {
        if ([strAction isEqualToString:@"open"])
        {
            if ([userInfo objectForKey:@"info_id"] == nil)
            {//栏目跳转
                NSString *strCateID = [userInfo objectForKey:@"cate_id"];
                //    [_mainController changeCategoryWithID:[strCateID intValue]];
                [rootview changeCategoryWithID:[strCateID intValue]];
                
            } else
            {//打开指定栏目的指定信息
                NSString *strCateID = [userInfo objectForKey:@"cate_id"];
                NSString *strInfoID = [userInfo objectForKey:@"info_id"];
                
                //跳转栏目
                [rootview changeCategoryWithID:[strCateID intValue]];
                
                
                //打开详情
                YZCellMassgeModel  *yzmodel =[[YZCellMassgeModel alloc]init];
                yzmodel.CateId =strCateID;
                yzmodel.id_massge =strInfoID;
                
                MassgeViewModelVC  *massgeview = [[MassgeViewModelVC alloc]initWithEstablish:yzmodel];
                [rootview.navigationController pushViewController:massgeview animated:YES];
                
                
                
                //   [_mainController openCate:[strCateID intValue] info:[strInfoID intValue]];
            }
        } else if ([strAction isEqualToString:@"open_url"])
        {
            if ([userInfo objectForKey:@"url"] != nil)
            {//跳转到指定链接
                NSString *url =[userInfo objectForKey:@"url"];
                PushWebViewUrl  *psuhUrlView = [[PushWebViewUrl alloc]init];
                psuhUrlView.URLStr =url;
                [rootview.navigationController pushViewController:psuhUrlView animated:YES];
                
                //   [_mainController openUrl:[userInfo objectForKey:@"url"]];
            }
        } else if ([strAction isEqualToString:@"read"])
        {
            NSString *strInfoType = [userInfo objectForKey:@"info_type"];
            if (!StringIsEmpty(strInfoType) && [strInfoType isEqualToString:@"topicmessage"])
            {//跳转到指定留言
                NSString *strInfoID = [userInfo objectForKey:@"info_id"];
                
                if([[XSZ_UICommon useridChangUaerdfLoginBoolString] isEqualToString:@""])
                {
                    //未登录 去登陆
                    LoginViewController  *LoginView =[[LoginViewController alloc]init];
                    LoginView.Viewdegat =rootview;
                    [rootview.navigationController pushViewController:LoginView animated:YES];
                    
                }else if (![[XSZ_UICommon useridChangUaerdfLoginBoolString] isEqualToString:@""])
                {
                    //已经登录 去留言管理
                    ReadTheMessageVIew  *readmassage =[[ReadTheMessageVIew alloc]init];
                    readmassage.id_massge = strInfoID;
                    [rootview.navigationController pushViewController:readmassage animated:YES];
                    
                }
                
                //   [_mainController openLeaveMsgWithID:[strInfoID intValue]];
            }
        } else if ([strAction isEqualToString:@"open_external_url"])
        {
            NSString *openUrl = [userInfo objectForKey:@"url"];
            if (!StringIsEmpty(openUrl))
            {
                
                
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:openUrl]];
                
                [UIApplication sharedApplication].applicationIconBadgeNumber=0;
            }
        }
    }
}


#pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        [self dealAPNSNewsWith:self.appLaunchAPNNews];
    }
}


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    // Required
    [APService registerDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    // 取得 APNs 标准信息内容
    NSDictionary *aps = [userInfo valueForKey:@"aps"];
    NSString *content = [aps valueForKey:@"alert"]; //推送显示的内容
    NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge数量
    NSString *sound = [aps valueForKey:@"sound"]; //播放的声音
        
    // 取得自定义字段内容
    NSString *customizeField1 = [userInfo valueForKey:@"customizeField1"]; //自定义参数，key是自己定义的
    NSLog(@"content =[%@], badge=[%d], sound=[%@], customize field  =[%@]",content,badge,sound,customizeField1);
    

    // Required
    [APService handleRemoteNotification:userInfo];

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    if (!_appIsActive)
    {
        [self dealAPNSNewsWith:userInfo];
    }
    
    HXLog(@"%@", userInfo);
}





- (void)applicationWillResignActive:(UIApplication *)application {
   
    
    _appIsActive = NO;
}

- (void)applicationDidEnterBackground:(UIApplication *)application {

    
  //  [HXTool saveAppEnterBackgroundDate:[self appEnterBackgroundDate]];
    _appIsActive = NO;
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

    
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
   
    
    _appIsActive = NO;
    [application setApplicationIconBadgeNumber:0];
    [application cancelAllLocalNotifications];
    
    
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    
    
     _appIsActive = YES;
}

- (void)applicationWillTerminate:(UIApplication *)application {
   
    
    
    
}



@end
